package com.mathlearning.auth.controller;

import com.mathlearning.auth.common.ApiResponse;
import com.mathlearning.auth.dto.learning.*;
import com.mathlearning.auth.service.UserLearningService;
import com.mathlearning.auth.util.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/user")
@Slf4j
public class UserLearningController {

    @Autowired
    private UserLearningService userLearningService;

    @Autowired
    private JwtUtil jwtUtil;

    /**
     * 获取当前用户ID
     */
    private Long getCurrentUserId(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if (token != null && token.startsWith("Bearer ")) {
            token = token.substring(7);
        }
        return jwtUtil.getUserIdFromToken(token);
    }

    /**
     * 获取学习统计数据
     * GET /api/user/learning-stats
     */
    @GetMapping("/learning-stats")
    public ApiResponse<LearningStatsResponse> getLearningStats(HttpServletRequest request) {
        try {
            Long userId = getCurrentUserId(request);
            LearningStatsResponse stats = userLearningService.getLearningStats(userId);
            return ApiResponse.success("获取成功", stats);
        } catch (Exception e) {
            log.error("获取学习统计失败", e);
            return ApiResponse.error(500, "获取学习统计失败: " + e.getMessage());
        }
    }

    /**
     * 获取我的学习记录
     * GET /api/user/my-learning
     */
    @GetMapping("/my-learning")
    public ApiResponse<Map<String, Object>> getMyLearning(HttpServletRequest request) {
        try {
            Long userId = getCurrentUserId(request);
            Map<String, Object> result = userLearningService.getMyLearning(userId);
            return ApiResponse.success("获取成功", result);
        } catch (Exception e) {
            log.error("获取学习记录失败", e);
            return ApiResponse.error(500, "获取学习记录失败: " + e.getMessage());
        }
    }

    /**
     * 获取帖子统计数据
     * GET /api/user/posts-stats
     */
    @GetMapping("/posts-stats")
    public ApiResponse<PostsStatsResponse> getPostsStats(HttpServletRequest request) {
        try {
            Long userId = getCurrentUserId(request);
            PostsStatsResponse stats = userLearningService.getPostsStats(userId);
            return ApiResponse.success("获取成功", stats);
        } catch (Exception e) {
            log.error("获取帖子统计失败", e);
            return ApiResponse.error(500, "获取帖子统计失败: " + e.getMessage());
        }
    }

    /**
     * 获取我的帖子列表
     * GET /api/user/my-posts
     */
    @GetMapping("/my-posts")
    public ApiResponse<Map<String, Object>> getMyPosts(HttpServletRequest request) {
        try {
            Long userId = getCurrentUserId(request);
            Map<String, Object> result = userLearningService.getMyPosts(userId);
            return ApiResponse.success("获取成功", result);
        } catch (Exception e) {
            log.error("获取帖子列表失败", e);
            return ApiResponse.error(500, "获取帖子列表失败: " + e.getMessage());
        }
    }

    /**
     * 获取用户学习记录日期列表（日历功能）
     * GET /api/user/learning-records/dates?year=2025&month=11
     */
    @GetMapping("/learning-records/dates")
    public ApiResponse<LearningDatesResponse> getLearningRecordDates(
            @RequestParam(required = false) Integer year,
            @RequestParam(required = false) Integer month,
            HttpServletRequest request) {
        try {
            Long userId = getCurrentUserId(request);
            LearningDatesResponse response = userLearningService.getLearningRecordDates(userId, year, month);
            return ApiResponse.success("获取成功", response);
        } catch (Exception e) {
            log.error("获取学习记录日期失败", e);
            return ApiResponse.error(500, "获取学习记录日期失败: " + e.getMessage());
        }
    }
}



